<?php
declare(strict_types=1);

namespace App\Service;

class RoomService
{
    /**
     * 所有房间id
     */
    const SYSTEM_ROOM_IDS = 'system_room_idx';

    /**
     * 房间信息
     */
    const ROOM_INFO = 'room_info';

    /**
     * 房间人员
     */
    const ROOM_USER = 'room_user';

    /*TODO *********************************************************Redis数据*******************************************************************↓↓↓↓↓*/


    /**
     * 获取房间
     * @param $room_id
     * @return array
     */
    public static function getRoom($room_id): array
    {
        return redis()->hGetAll(self::ROOM_INFO . $room_id);
    }

    /**
     * 获取房间号
     * @return int
     */
    public static function getRoomId(): int
    {
        $redis = redis();
        $room_id = $redis->get('room_idx');
        if (!$room_id) {
            $room_id = 100000;
            $redis->set('room_idx', $room_id);
        }
        return (int)$room_id;
    }

    /**
     * 获取房间用户
     * @param $room_id
     * @return array
     */
    public static function getRoomUser($room_id): array
    {
        $list = [];
        $uids = redis()->sMembers(self::ROOM_USER . $room_id);
        if ($uids) {
            foreach ($uids as $uid) {
                $user = UserService::getUserRedis($uid);
                $list[$uid] = $user;
            }
        }
        return $list;
    }

    /**
     * @param $room_id
     * @return bool
     */
    public static function updateRoomId($room_id): bool
    {
        return redis()->set('room_idx', $room_id);
    }

    /**
     * 更新房间信息
     * @param $room_id
     * @param $data
     * @return bool
     */
    public static function updateRoom($room_id, $data): bool
    {
        return redis()->hMSet(self::ROOM_INFO . $room_id, $data);
    }

    /**
     * 添加用户到房间
     * @param $uid
     * @param $room_id
     * @return bool|int
     */
    public static function addUserToRoom($uid, $room_id)
    {
        return redis()->sAdd(self::ROOM_USER . $room_id, $uid);
    }

    /**
     * 房间移除用户
     * @param $user
     * @return int | bool
     */
    public static function kickUser($user)
    {
        $room_id = $user['room_id'] ?? 0;
        if (!$room_id) {
            return false;
        }
        $table = $user['table'] ?? 0;
        UserService::updateUserRedis($user['uid'], ['room_id' => 0, 'table' => 0]);
        $room = RoomService::getRoom($user['room_id']);
        if (!$room) {
            return false;
        }
        //更新房间
        $update = ['count' => $room['count'] - 1];
        if ($table) {
            $update['table'.$table] = 0;
        }
        RoomService::updateRoom($room_id, $update);
        return redis()->sRem(self::ROOM_USER . $room_id, $user['uid']);
    }

    /**
     * 添加房间到系统集合
     * @param $room_id
     * @return bool|int
     */
    public static function addRoomID($room_id)
    {
        return redis()->sAdd(self::SYSTEM_ROOM_IDS, $room_id);
    }

    /**
     * 获取可用的桌子
     * @param array $room
     * @return string|null
     */
    public static function getEnableTable(array $room): ?string
    {
        //桌子
        if (isset($room['table1']) && $room['table1']) {
            return '1';
        } else if (isset($room['table2']) && $room['table2']) {
            return '2';
        } else if (isset($room['table3']) && $room['table3']) {
            return '3';
        } else if (isset($room['table4']) && $room['table4']) {
            return '4';
        } else {
            return null;
        }
    }

}